package vpub

import (
	"context"
	"flag"
	"fmt"
	"svvenus/conf"
	"svvenus/util/env"
	"svvenus/vpub/constants"
	"svvenus/vpub/db/dbm"
	"svvenus/zlog"
)

var (
	Ctx    context.Context    //告知该链接已经退出/停止的channel
	Cancel context.CancelFunc //退出函数
)

// InitLead 最先的初始化
// @return bool
func InitLead() bool {
	fmt.Println("InitLead")
	var err error

	//配置初始化
	//conf.InitViper()
	conf.InitJson()

	//从命令行参数获取服务器ID
	flag.IntVar(&constants.ServerId, "id", 0, "命令行获取服务器ID")
	flag.Parse()
	fmt.Println("服务器ID：", constants.ServerId)

	if !conf.InitExtPublic(constants.ServerId) {
		return false
	}

	//环境
	env.Set(conf.PubConfServer.ServerMode)
	fmt.Println("env", env.Get())

	// 日志系统初始化
	zlog.LogInit(env.IsDev())

	//链接数据库
	constants.MgoMain, err = dbm.NewMgoBase(conf.PubConfServer.MongoHost, conf.PubConfServer.MongoPort, conf.PubConfServer.MongoDatabase)
	if err != nil {
		zlog.Warn(err)
		return false
	}
	constants.MgoRecord, err = dbm.NewMgoBase(conf.PubConfServer.MongoHost, conf.PubConfServer.MongoPort, conf.PubConfServer.MongoDatabase+"_rec")
	if err != nil {
		zlog.Warn(err)
		return false
	}

	constants.PortTcp = conf.PubConfServer.ServerPort
	constants.PortHttp = constants.PortTcp + 1
	constants.ServerLocked = conf.PubConfServer.Locked

	return true
}
